﻿#region Using directives

using System;
using System.Xml;
using MCMLXVII.SubSystems.Security.DirectoryServices;
using MCMLXVII.BackEnd.Pipeline;
using MCMLXVII.BackEnd.Data.SystemDatabase;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.BackEnd.Services.SystemConfiguration.Messages;
using MCMLXVII.Core.Common.BackEnd;
using MCMLXVII.BackEnd.Data.SystemDatabase.DataSets;
using System.ServiceModel;
#endregion

namespace MCMLXVII.BackEnd.Services.Core.BusinessActions
{
    public static class DirectoryServices
    {

        public static msgSecurityProvider[] GetEnabledSecurityProviders(msgVoidRequest req)
        {
            int MemberID = Security.GetMemberIDFromSession();
            DSSecurityProviders.SecurityProvidersAndClassDataTable dt = Security.GetEnabledSecurityProviders( MemberID );
            msgSecurityProvider[] rsp = new msgSecurityProvider[dt.Rows.Count];
            int i = 0;
            foreach (DSSecurityProviders.SecurityProvidersAndClassRow row in dt.Rows)
            {
                rsp[i] = new msgSecurityProvider(row.SecProviderID, row.SecProviderClassID, row.Description, row.FQDN, row.NetBiosName,(row.SupportsTreeBrowsing=="Y"), (row.SupportsGroups=="Y"), (row.SupportsCrawling=="Y"));
                i++;
            }
            return rsp;
        }

        public static msgXMLResponse GetDirectoryTree(msgGenericItemRequest<int> req)
        {
            
            DSSecurityProviders.SecurityProvidersAndClassRow Inf = Security.GetDomainInfo(req.Item) ;
            if (Inf == null)
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional, Properties.Resources.BadDomainID));
            Type DirSvcType = System.Type.GetType(Inf.AssemblyAndClass, true);
            if (Inf.SupportsTreeBrowsing == "Y")
            {
                IDirectoryTreeBrowser DirSvc = (IDirectoryTreeBrowser)Activator.CreateInstance(DirSvcType);
                XmlDocument info = new XmlDocument();
                info.LoadXml(Inf.AditionalInfo);
                XmlDocument doc = DirSvc.GetDirectoryTree(Inf.FQDN, Inf.NetBiosName, info, 50);
                return new msgXMLResponse(doc);
            }
            else
            {
                
                IDirectoryService DirSvc = (IDirectoryService)Activator.CreateInstance(DirSvcType);
                string s = "<Root Name=\"" + Inf.NetBiosName + "\" Path=\"" + DirSvc.GetRoot(Inf.FQDN, Inf.NetBiosName) + "\" Expanded=\"false\" />";
                return new msgXMLResponse(s);
            }

        }

        public static msgXMLResponse GetDirectoryNodeUsers(msgSecurityUsersListRequest req)
        {

            DSSecurityProviders.SecurityProvidersAndClassRow Inf = Security.GetDomainInfo(req.ProviderID);
            if (Inf == null)
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional, Properties.Resources.BadDomainID));
            Type DirSvcType = System.Type.GetType(Inf.AssemblyAndClass, true);
            IDirectoryService DirSvc = (IDirectoryService)Activator.CreateInstance(DirSvcType);
            string path;
            if (Inf.SupportsTreeBrowsing == "Y")
                path = req.ProviderPath;
            else
                path = DirSvc.GetRoot(Inf.FQDN, Inf.NetBiosName);
            XmlDocument info = new XmlDocument();
            info.LoadXml(Inf.AditionalInfo);
            XmlDocument doc = DirSvc.GetDirectoryUsersList(path, info, req.Filter); 
            return new msgXMLResponse(doc);
        }

        public static msgXMLResponse GetDirectoryNodeGroups(msgSecurityUsersListRequest req)
        {

            DSSecurityProviders.SecurityProvidersAndClassRow Inf = Security.GetDomainInfo(req.ProviderID);
            if (Inf == null)
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional, Properties.Resources.BadDomainID));
            Type DirSvcType = System.Type.GetType(Inf.AssemblyAndClass, true);
            IDirectoryService DirSvc = (IDirectoryService)Activator.CreateInstance(DirSvcType);
            string path;
            if (Inf.SupportsTreeBrowsing == "Y")
                path = req.ProviderPath;
            else
                path = DirSvc.GetRoot(Inf.FQDN, Inf.NetBiosName);
            XmlDocument info = new XmlDocument();
            info.LoadXml(Inf.AditionalInfo);
            XmlDocument doc = DirSvc.GetDirectoryGroupsList(path, info, req.Filter);
            return new msgXMLResponse(doc);
        }


    }
}
